<?php

namespace app\admin\controller;

use app\admin\exception\AdminException;
use app\common\entity\Export;
use app\common\entity\Rushgoodslist;
use think\Db;
use think\Request;
use app\common\entity\User;

class Rushgoods extends Admin
{

    /**
     * @power 内容管理|商品管理
     * @rank 0
     */
    public function index()
    {
        $categoryId = input('get.type');
        $goodsModel = new \app\common\entity\Rushgoods();
        //商品列表
        $goodsLists = $goodsModel->getGoodsPages($categoryId);

        return $this->render('index', [
            'goodsLists' => $goodsLists,
            'cate' => \app\common\entity\GoodsCategory::getAllCate(),
        ]);
    }
    /**
     * 参与者列表
     */
    public function userlist($id,Request $request)
    {
        $goodsModel = new \app\common\entity\Rushgoodslist();
        $getid = $request->get($id);
        if ($getid){
            $id = $getid;
        }
        $map['id'] = $id;
        $entity = $goodsModel->alias('um')->field('um.*,u.mobile,u.nick_name');
        if ($keyword = $request->get('keyword')) {
            $entity->where('u.nick_name', $keyword);
            $map['keyword'] = $keyword;
        }

        $userTable = (new \app\common\entity\User())->getTable();

        $goodsLists = $entity->leftJoin("{$userTable} u", 'um.user_id = u.id')
            ->where('um.rush_order_id',$id)
            ->order('um.createtime', 'desc')
            ->paginate(20, false, [
                'query' => isset($map) ? $map : []
            ]);

        // $categoryId = input('get.type');
        //商品列表
//        $goodsLists = $goodsModel->getGoodsPages($id);
        return $this->render('userlist', [
            'goodsLists' => $goodsLists,
        ]);
    }

    public function win_yes($rush_order_id,$id)
    {
        $rushGoodsListModel = new Rushgoodslist();
        $res = $rushGoodsListModel->where('id', $id)->setField('is_win',1);
        $upd = $rushGoodsListModel->where('rush_order_id',$rush_order_id)->where('id','<>',$id)->setField('is_win',0);
        return json(['code' => 0, 'message' => '操作成功']);


    }

    public function win_no($rush_order_id)
    {
        $rushGoodsListModel = new Rushgoodslist();
        $upd = $rushGoodsListModel->where('rush_order_id',$rush_order_id)->setField('is_win',0);
        return json(['code' => 0, 'message' => '操作成功']);


    }


    public function usersend($id)
    {
        $entity = \app\common\entity\Rushgoods::where('id', $id)->find();
//        dump($entity);die;
        if (!$entity) {
            $this->error('用户对象不存在');
        }

        return $this->render('usersend', [
            'info' => $entity,
        ]);
    }
    public function updateSave(Request $request, $id)
    {

        // $res = $this->validate($request->post(), 'app\admin\validate\Goods');

        // if (true !== $res) {
        //     return json()->data(['code' => 1, 'message' => $res]);
        // }
            if(empty($request->post('odd_company')) || empty($request->post('odd_number'))){
                return json()->data(['code' => 1, 'message' => '请填写信息']);
            }

        $entity = $this->checkInfo($id);

        $service = new \app\common\entity\Rushgoods();
        $result = $service->updateGoodsSend($entity, $request->post());

        if (!$result) {
            throw new AdminException('保存失败');
        }

        return json(['code' => 0, 'toUrl' => url('/admin/rushgoods')]);
    }
    /**
     * @power 内容管理|商品管理@添加商品
     */
    public function create()
    {
        return $this->render('edit', [
            'cate' => \app\common\entity\GoodsCategory::getAllCate(),
            // 'levelcate' => \app\common\entity\UserPackage::getAllCate(),

        ]);
    }

    /**
     * @power 内容管理|商品管理@编辑商品
     */
    public function edit($id)
    {
        $entity = \app\common\entity\Rushgoods::where('id', $id)->find();
        $entity['goods_pic'] = json_decode($entity['goods_pic']);
//        dump($entity);die;
        if (!$entity) {
            $this->error('用户对象不存在');
        }

        return $this->render('edit', [
            'info' => $entity,
            'cate' => \app\common\entity\GoodsCategory::getAllCate(),
            'levelcate' => \app\common\entity\UserPackage::getAllCate(),

        ]);
    }



    /**
     * @power 修改期号
     */
    public function stagehao($id)
    {
        $entity = \app\common\entity\Rushgoods::where('id', $id)->find();
        $entity['goods_pic'] = json_decode($entity['goods_pic']);

        if (!$entity) {
            $this->error('用户对象不存在');
        }

        return $this->render('edit', [
            'info' => $entity,
            'cate' => \app\common\entity\GoodsCategory::getAllCate(),
            'levelcate' => \app\common\entity\UserPackage::getAllCate(),

        ]);
    }

    /**
     * @power 内容管理|商品管理@添加商品
     */
    public function save(Request $request)
    {
       
        // $res = $this->validate($request->post(), 'app\admin\validate\Goods');

        // if (true !== $res) {
        //     return json()->data(['code' => 1, 'message' => $res]);
        // }
        $service = new \app\common\entity\Rushgoods();
        $result = $service->addGoods($request->post());

        if (!$result) {
            throw new AdminException('保存失败');
        }
        //添加用户提醒
        if ($request->post('category') == 1 && $request->post('status') == 1) {
            User::update(['notice' => 1], ['notice' => 0]);
        }

        return json(['code' => 0, 'toUrl' => url('/admin/rushgoods')]);
    }

    /**
     * @power 内容管理|商品管理@编辑商品
     */
    public function update(Request $request, $id)
    {

        // $res = $this->validate($request->post(), 'app\admin\validate\Goods');

        // if (true !== $res) {
        //     return json()->data(['code' => 1, 'message' => $res]);
        // }


        $entity = $this->checkInfo($id);
    
        $service = new \app\common\entity\Rushgoods();
        $list = $request->post();
        

        $list['goods_total_need_integral'] = $list['goods_total_number'] * $list['goods_need_integral'];
        
        
        $result= DB::table("rush_order")->where('id',$id)->update($list);
        // $result = $service->updateGoods($entity, $request->post());
        

        if (!is_int($result)) {
            throw new AdminException('保存失败');
        }

        return json(['code' => 0, 'toUrl' => url('/admin/rushgoods')]);
    }


    /**
     * @power 内容管理|商品管理@删除商品
     */
    public function delete(Request $request, $id)
    {
        $entity = $this->checkInfo($id);

        if (!$entity->delete()) {
            throw new AdminException('删除失败');
        }

        return json(['code' => 0, 'message' => 'success']);
    }
    public function open(Request $request, $id)
    {
        $entity = $this->checkInfo($id);
        $entity->status = 1;
        if (!$entity->save()) {
            throw new AdminException('开启失败');
        }

        return json(['code' => 0, 'message' => 'success']);
    }
    private function checkInfo($id)
    {
        $entity = \app\common\entity\Rushgoods::where('id', $id)->find();
        if (!$entity) {
            throw new AdminException('对象不存在');
        }

        return $entity;
    }

    /**
     * @power 内容管理|商品管理@添加商品分类
     */
    public function createCategory()
    {
        return $this->render('category', [
            'cate' => \app\common\entity\Goods::getAllCate()
        ]);
    }

    /**
     * @power 内容管理|商品管理@添加商品分类
     */
    public function categorySave(Request $request)
    {
        $res = $this->validate($request->post(), 'app\admin\validate\CategoryForm');

        if (true !== $res) {
            return json()->data(['code' => 1, 'message' => $res]);
        }

        $service = new \app\common\entity\GoodsCategory();
        $result = $service->addGoodsCategoryParent($request->post());


        if (!$result) {
            throw new AdminException('保存失败');
        }

        return json(['code' => 0, 'toUrl' => url('/admin/shop')]);
    }


}
